package gui.display;

import java.awt.BorderLayout;
import java.awt.Color;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;

/**
 * Abstract class describing a display, which holds an
 * output window and a input field.
 */
public abstract class Display extends JPanel
{
    public static final String REGULAR = "Regular";
    public static final String ITALIC = "Italic";
    public static final String BOLD = "Bold";

    public static final String BLUE = "Blue";
    public static final String BLUEBOLD = "BlueBold";
    public static final String BLUEBOLDITALIC = "BlueBoldItalic";

    public static final String DARKGREY = "DARKGREY";
    
    private final JTextPane textPane_ = new JTextPane();
    private final JScrollPane scrollPane_ = new JScrollPane( textPane_ );
    protected final JTextField inputField_ = new JTextField();

    private static final SimpleDateFormat sdf = new SimpleDateFormat( "(HH:mm:ss) " );
    
    public Display()
    {
        super( new BorderLayout() );

        StyledDocument doc = textPane_.getStyledDocument();
        Style style = StyleContext.getDefaultStyleContext().getStyle( StyleContext.DEFAULT_STYLE );

        // REGULAR
        style = doc.addStyle( REGULAR, null );
        StyleConstants.setForeground( style, Color.lightGray );

        // ITALIC
        style = doc.addStyle( ITALIC, null );
        StyleConstants.setForeground( style, Color.green );
        StyleConstants.setItalic( style, true );

        // BOLD
        style = doc.addStyle( BOLD, null );
        StyleConstants.setForeground( style, Color.green );
        StyleConstants.setBold( style, true );
        
        // BLUE
        style = doc.addStyle( BLUE, null );
        StyleConstants.setForeground( style, new Color( 100, 100, 255 ) );

        // BLUEBOLD
        style = doc.addStyle( BLUEBOLD, null );
        StyleConstants.setForeground( style, new Color( 100, 100, 255 ) );
        StyleConstants.setBold( style, true );

        // BLUEBOLDITALIC
        style = doc.addStyle( BLUEBOLDITALIC, null );
        StyleConstants.setForeground( style, new Color( 100, 100, 255 ) );
        StyleConstants.setBold( style, true );
        StyleConstants.setItalic( style, true );

        // DARKGREY
        style = doc.addStyle( DARKGREY, null );
        StyleConstants.setForeground( style, Color.lightGray );
        
        textPane_.setEditable( false );
        textPane_.setBackground( new Color( 43, 43, 43 ) );
        scrollPane_.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED );

        add( scrollPane_, BorderLayout.CENTER );
        add( inputField_, BorderLayout.SOUTH );
    }

    public void printStyledText( final String text, final String style )
    {
        String t = text;

        // UTF8 -> Latin1...really ugly, but will have to do
        t = t.replaceAll( "Ã¥", "å" );
        t = t.replaceAll( "Ã¤", "ä" );
        t = t.replaceAll( "Ã¶", "ö" );
        
        t = t.replaceAll( "Ã", "Å" );
        t = t.replaceAll( "Ã", "Ä" );
        t = t.replaceAll( "Ã", "Ö" );
        
        try
        {
            StyledDocument doc = textPane_.getStyledDocument();
            /*try
            {*/
                doc.insertString(doc.getLength(), t, textPane_.getStyle(style));
            /*}
            catch (UnsupportedEncodingException ex)
            {
                doc.insertString(doc.getLength(), text, _textPane.getStyle(style));
                Logger.getLogger(Display.class.getName()).log(Level.SEVERE, null, ex);
            }*/
        }
        catch ( BadLocationException ex )
        {
            // Silence is golden
        }
    }

    public final void printTimeStamp()
    {
        printStyledText( sdf.format( Calendar.getInstance().getTime() ), DARKGREY );
    }

    public final void printWarningText( final String text )
    {
        printStyledText( text, ITALIC );
    }

    public final void printNewLine()
    {
        printStyledText( "\n", REGULAR );
        
        validate();

        JScrollBar scroll = scrollPane_.getVerticalScrollBar();
        scroll.setValue( scroll.getMaximum() );
    }

    public abstract void print( String message );
    public abstract void userPrint( String message );
}
